<?php

/**
 * @version		$Id: hello.php 15 2009-11-02 18:37:15Z chdemko $
 * @package		Joomla16.Tutorials
 * @subpackage	Components
 * @copyright	Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
 * @author		Christophe Demko
 * @link		http://joomlacode.org/gf/project/helloworld_1_6/
 * @license		License GNU General Public License version 2 or later
 */

// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// import Joomla modelitem library
jimport('joomla.application.component.modelitem');

/**
 * HelloWorld Model
 */
class bgModelbg extends JModel
{
    var $_total = null;
    var $_pagination = null;
    
    public function __construct(){
    	parent::__construct();
        $mainframe = JFactory::getApplication();
        // Get pagination request variables
        $limit = $mainframe->getUserStateFromRequest('com_question.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
        $limitstart = JRequest::getVar('limitstart', 0, '', 'int');
 
        // In case limit has been changed, adjust it
        $limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);
        $this->setState('limit', $limit);
        $this->setState('limitstart', $limitstart);
    }
    
    /**
	*Get Total
	*/
    function getTotal($where = null){
        //Load the content if it doesn't already exist
        if($this->_total === null){
            $query = "SELECT COUNT(*) FROM ques $where";
            $this->_db->setQuery($query);
            $this->_total = $this->_db->loadResult();
        }
        return $this->_total;
    }
    
    /**
	*Get Pagination
	*/
    function getPagination(){
        // Load the content if it doesn't already exist
        if (empty($this->_pagination)) {
            jimport('joomla.html.pagination');
            $this->_pagination = new JPagination($this->getTotal(), $this->getState('limitstart'), $this->getState('limit') );
        }
        return $this->_pagination;
    }
    
    /**
	*Get data table QUES add in list
	*/
    function getQUES(){
        $keyword = JRequest::getVar('keyword');
        $where = "WHERE QUES_CODE = '$keyword' OR QUES_TITLE LIKE '%$keyword%' ";
        
        $sql = "SELECT QUES_CODE,QUES_TITLE,QUES_TYPE,QUES_SERQ_CODE,QUES_PAGQ_CODE,QUES_REQUIRED,QUES_PUBLISHED
                FROM ques ";
        
        $this->_total = $this->getTotal($where);
        $sql .= $where;
        //Order by
        $sql .= " ORDER BY QUES_CODE DESC";
        
        //Pagination
        $sql .= " LIMIT ".$this->getState('limitstart').",".$this->getState('limit');
        $this->_db->setQuery($sql);
        return $this->_db->loadObjectList();
    }
    
    /**
	*Load data SERQ_TITLE in table SERQ
	*/
	function getSERQ($id){
       $qr = "SELECT *
                FROM serq
                WHERE SERQ_CODE = '$id' ";
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObject();
       return $result;
	}
    
    /**
	*Load data PAGQ_TITLE in table PAGQ
	*/
	function getPAGQ($id){
       $qr = "SELECT PAGQ_TITLE
                FROM pagq
                WHERE PAGQ_CODE = '$id' ";
       $this->_db->setQuery($qr);
       $result = $this->_db->loadResult();
       return $result;
	} 
    
    /**
	*Delete information QUES
	*/
    function remove($id){
        $id = implode(',', $id);
        $qr = "DELETE FROM ques WHERE QUES_CODE IN($id)";
        $this->_db->setQuery($qr);
        if($this->_db->query()){
            $qr = "DELETE FROM ansq WHERE ANSQ_QUES_CODE IN ($id)";
            $this->_db->setQuery($qr);
            if($this->_db->query()){
                return true;
            }
        }
        else{
            return false;
        }
    }
    
    /**
     * Remove ANSQ from QUES_CODE 
    */
    public function removeansq($cid){
        $cid = implode(',', $cid);
    
        $query = "DELETE FROM ansq WHERE ANSQ_QUES_CODE IN($cid)";
  
        $this->_db->setQuery($query);
        $this->_db->query();
    }
    
    
    /**
	* Load data SERQ_FULLDOWN
	*/
    function getSERQ_FULLDOWN(){
        $this->_db->setQuery("SELECT SERQ_CODE,SERQ_TITLE FROM serq WHERE SERQ_FLAG = 1");             
        return $this->_db->loadObjectList();  
    }  
    
    /**
	* Load data PAGQ
	*/
    public function getPAGQ_FULLDOWN($id){
        $this->_db->setQuery("SELECT PAGQ_CODE,PAGQ_TITLE FROM pagq WHERE PAGQ_SERQ_CODE = $id ORDER BY PAGQ_CODE DESC");             
        return $this->_db->loadObjectList();  
    }   
    
    /**
	* Insert information QUES
	*/
    function insertQues($ques,$ansq,$addNew){
        $id = HelperBG::getIDAuto('ques','QUES_CODE');
        if($addNew == true){
            $qr = "INSERT INTO ques (QUES_CODE,QUES_SERQ_CODE,QUES_TITLE,QUES_REQUIRED,QUES_DESCRIPTION,QUES_TYPE,QUES_PAGQ_CODE,QUES_PUBLISHED) 
                VALUES ($id,$ques[QUES_SERQ_CODE],'$ques[QUES_TITLE]',$ques[QUES_REQUIRED],'$ques[QUES_DESCRIPTION]',$ques[QUES_TYPE],$ques[QUES_PAGQ_CODE],1)";
            $this->_db->setQuery($qr);
            if($this->_db->query()){
                foreach ($ansq as $value) {
                    $ANSQ_CODE = HelperBG::getIDAuto('ansq','ANSQ_CODE');
                    $this->_db->setQuery("INSERT INTO ansq (ANSQ_CODE,ANSQ_QUES_CODE,ANSQ_VALUE) 
                                            VALUES ($ANSQ_CODE,$id,'$value')");      
                    $this->_db->query();
                }
                return true;
            }
            else{
                return false;
            }
        }
        else{
            $qr = "UPDATE ques SET 
                    QUES_SERQ_CODE = $ques[QUES_SERQ_CODE]
                    ,QUES_TITLE = '$ques[QUES_TITLE]'
                    ,QUES_REQUIRED = $ques[QUES_REQUIRED]
                    ,QUES_DESCRIPTION = '$ques[QUES_DESCRIPTION]'
                    ,QUES_TYPE = $ques[QUES_TYPE]
                    ,QUES_PAGQ_CODE = $ques[QUES_PAGQ_CODE]
                    ,QUES_PUBLISHED = 1
                    WHERE QUES_CODE = $ques[QUES_CODE]";
            $this->_db->setQuery($qr);
            if($this->_db->query()){
                //Delete ket qua truoc khi cap nhat lai
                $this->_db->setQuery("DELETE FROM ansq WHERE ANSQ_QUES_CODE IN ($ques[QUES_CODE])");      
                $this->_db->query();
                foreach ($ansq as $value) {
                    $ANSQ_CODE = HelperBG::getIDAuto('ansq','ANSQ_CODE');
                    $this->_db->setQuery("INSERT INTO ansq (ANSQ_CODE,ANSQ_QUES_CODE,ANSQ_VALUE) 
                                            VALUES ($ANSQ_CODE,$ques[QUES_CODE],'$value')");      
                    $this->_db->query();
                }
                return true;
            }
            else{
                return false;
            }
        }
    }
    
    /**
	* Load data QUES
	*/
    function load($cid){
        $qr = "SELECT * FROM ques WHERE QUES_CODE = $cid";
        $this->_db->setQuery($qr);
        $result = $this->_db->loadObject();
        return $result;
    }
    
    /**
	* Load ANSQ theo QUES_CODE
	*/
    function loadANSQ($cid){
        $qr = "SELECT ANSQ_VALUE FROM ansq WHERE ANSQ_QUES_CODE = $cid ORDER BY ANSQ_CODE";
        $this->_db->setQuery($qr);
        $result = $this->_db->loadObjectList();
        return $result;
    }
    
    /**
	*Check QUES_CODE exist
	*/
    function checkExistID($cid){
        $qr = "SELECT QUES_CODE FROM ques WHERE QUES_CODE = $cid";
        $this->_db->setQuery($qr);
        $result = $this->_db->loadObject();
        if(!empty($result)){ 
            return true;
        }
        return false;
    }
}
